﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Model;

namespace BLL
{
    public class ProjectNumberBLL 
    { 
       
        /// <summary>
         /// 获取目前数据库中最后的项目编号
         /// </summary>
         /// <returns></returns>
        public string  GetLastNum()
        {
            var LastNum = new ProjectNumberDAL().GetLastNum();
            return  Assignment(LastNum.ProjectNumber);

        }
        #region LastNumStr目前数据库中最后的单号
		 

        public static String Assignment(String LastNumStr)
        {
            string number0 = "";
            DateTime date = System.DateTime.Now;
            string year = date.Year.ToString();
            string month = date.Month.ToString();
            string day = date.Day.ToString();
            if (month.Length < 2)
                month = '0' + month;
            if (day.Length < 2)
                day = '0' + day;
            string ymd = year + month + day;
            if (LastNumStr.Length < 8 || LastNumStr.Substring(0, 8) != ymd || LastNumStr.Substring(4, 2) != month || LastNumStr.Substring(0, 4) != year)
            {
                return ymd + "0001";
            }
            else
            {

                Int32 clientnumber = Convert.ToInt32(LastNumStr.Substring(8, LastNumStr.Length - 8)) + 1;
                if (clientnumber.ToString().Length > LastNumStr.Length - 8)
                {
                    return ymd + clientnumber.ToString();
                }
                else
                {
                    for (int i = 0; i < LastNumStr.Length - 8 - clientnumber.ToString().Length; i++)
                    {
                        number0 += "0";
                    }
                    return ymd + number0 + clientnumber.ToString();
                }
            }
        }
#endregion
    }
}
